/* eslint-disable no-undef */
import { editorToolBarUpload } from "../uploadimage/editor-toolbar-upload.js";
const uploadFile = (type = "image", option, callback) => {
    editorToolBarUpload({
        type: type,
        limitImage: option.limit,
        limitVideo: {},
        path: option.path,
        sure: (vm, model) => {
            callback && callback(vm, model);
        }
    });
};
tinymce.PluginManager.add("uploadvideo", function(editor) {
    const pluginName = "视频上传";
    const pluginOption = editor.getParam("uploadvideo_option", {});
    const option = {
        path: pluginOption.path || "",
        limit: pluginOption.val ? { value: pluginOption.val } : {}
    };
    editor.ui.registry.addButton("uploadvideo", {
        icon: "embed",
        tooltip: pluginName,
        onAction: function() {
            uploadFile("video", option, (vm, model) => {
                editor.insertContent(
                    `<img class="mce-object mce-object-video"
                        src="${model.cover.coverUrl}"
                        style="max-width:100%"
                        width="auto" height="auto"
                        data-mce-p-poster="${model.cover.coverUrl}"
                        data-mce-p-controls="controls"
                        data-mce-html='<source src="${model.cover.url}" type="video/mp4">'
                        data-mce-object="video"
                        data-mce-selected="1"></img>`
                );
                vm.close();
            });
        }
    });
    editor.ui.registry.addMenuItem("uploadvideo", {
        text: pluginName,
        onAction: function() {
            uploadFile("video", option, (vm, model) => {
                editor.insertContent(
                    `<img src="${model.cover.url}" alt="${model.keywords}" title="${model.keywords}" style="max-width: 100%;"></img>`
                );
                vm.close();
            });
        }
    });

    return {
        getMetadata: function() {
            return {
                name: pluginName,
                url: ""
            };
        }
    };
});
